package com.example.administrator.im.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.text.InputFilter;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.administrator.im.R;
import com.example.administrator.im.data.SearchFriendData;
import com.example.administrator.im.ui.activity.FriendDetailsActivity;
import com.example.administrator.im.utils.EMUtils;
import com.example.administrator.im.utils.HttpUtils;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * create:2018/10/20 14:04
 * author: 姚鸿杰
 **/
public class SearchFriendAdapter extends RecyclerView.Adapter<SearchFriendAdapter.Holder> {

    private List<SearchFriendData> mList;
    private Context mContext;

    private final String TAG = "SearchFriendAdapter";

    public SearchFriendAdapter(List<SearchFriendData> mList, Context mContext) {
        if (mList == null) {
            this.mList = new ArrayList<>();
        } else
            this.mList = mList;
        this.mContext = mContext;
    }

    public void setmList(List<SearchFriendData> mList) {
        if (mList == null) {
            this.mList = new ArrayList<>();
        } else
            this.mList = mList;
        notifyDataSetChanged();
    }

    public void setmList(List<SearchFriendData> mList, AddFriendListener listener) {
        if (mList == null) {
            this.mList = new ArrayList<>();
        } else
            this.mList = mList;
        addFriendListener = listener;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.recycler_search_friend, viewGroup, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {

        final SearchFriendData data = mList.get(position);

        SpannableStringBuilder spannable = new SpannableStringBuilder(data.getUsername());
        int startIndex = data.getUsername().indexOf(data.getContent());
        int endIndex = data.getContent().length();

        spannable.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.theme_color)),
                startIndex, startIndex + endIndex
                , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        holder.nickname.setText(mList.get(position).getNickname());
        holder.username.setText(spannable);
        String url = mList.get(position).getImg_path();

        if (!TextUtils.equals(url, HttpUtils.PORT)) {
            RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.login_logo)
                    .error(R.mipmap.login_logo)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .override(100, 100);
            Glide.with(mContext).load(url).apply(options).into(holder.head_img);
        }

        if (!TextUtils.isEmpty(data.getContent())) {
            holder.btn.setVisibility(View.GONE);
        } else {
            holder.btn.setVisibility(View.VISIBLE);
            //添加好友事件
            holder.btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Toast.makeText(mContext, "加好友", Toast.LENGTH_SHORT).show();
                    addFriendListener.addFriendListener();
                }
            });
        }
        holder.search_friend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!TextUtils.isEmpty(data.getContent())) {
                    Intent intent = new Intent(mContext, FriendDetailsActivity.class);
                    //传递参数
                    intent.putExtra("username", data.getUsername());
                    intent.putExtra("nickname", data.getNickname());
                    mContext.startActivity(intent);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    private AddFriendListener addFriendListener;

    public interface AddFriendListener {
        void addFriendListener();
    }

    public class Holder extends RecyclerView.ViewHolder {

        private ViewGroup search_friend;
        private CircleImageView head_img;
        private TextView nickname;
        private TextView username;
        private Button btn;

        public Holder(@NonNull View itemView) {
            super(itemView);

            search_friend = (ViewGroup) itemView.findViewById(R.id.search_friend);
            head_img = (CircleImageView) itemView.findViewById(R.id.search_img);
            nickname = (TextView) itemView.findViewById(R.id.search_nickname);
            username = (TextView) itemView.findViewById(R.id.search_username);
            btn = (Button) itemView.findViewById(R.id.add_friend);

        }
    }
}
